package edu.hhu.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;


import com.alibaba.fastjson.JSON;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import edu.hhu.basic.service.BasicService;
public class BasicController extends Controller {
	BasicService service = new BasicService();
	public void index() {
	//	String sql = "from pcdtsjtrue";
	//	int pageNum = getParaToInt("pageName", 1);
		//Page<Record> page = Db.use("mysql").paginate(1, 20, "select *", sql);
		//List<Record> tongji = Db.use("mysql").find("select * from config where type='tongji'");
		//List<Record> dynamic = Db.use("mysql").find("select * from config where type='dynamic'");
		//List<Record> st = Db.use("mysql").find("select * from config where type='static'");
		//List<Record> gj = Db.use("mysql").find("select * from config where yes='1'");
	//	List<Record> table = Db.use("mysql").find("select * from config");
		//this.setAttr("tongji", tongji);
		//this.setAttr("dynamic", dynamic);
		//this.setAttr("static", st);
		//this.setAttr("table", table);
		//this.setAttr("basic", page);
	//	this.setAttr("gj", gj);
		System.out.println("hhh");
		render("/basic/query.jsp");
		//render("/basic/query.jsp");
	}
	
	public void page() {
		String para = getPara(0);
		String sql = "from cjsctjtemp";
		int pageNum = 0;
		System.out.println(para);
		System.out.println();
		if (para.equals("first")) {
			pageNum = getParaToInt(1);
			System.out.println(pageNum);
		} else if (para.equals("down")) {
			pageNum = getParaToInt(1) + 1;
			System.out.println(pageNum);
		} else if (para.equals("up")) {
			pageNum = getParaToInt(1) - 1;
			System.out.println(pageNum);
		} else if (para.equals("last")) {
			pageNum = getParaToInt(1);
			System.out.println(pageNum);
		}
		Page<Record> page = Db.use("mysql").paginate(pageNum, 20, "select *",
				sql);
		this.setAttr("basic", page);
		String para2 = getPara("ziduan");
		System.out.println(para2);
		render("/basic/query.jsp");
	}

	public void query() {
		String para = null;
		try {
			para = URLDecoder.decode(getPara("id"), "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		System.out.println(para);
		List<Record> list = Db.use("mysql").find(
				"select * from cjscjt where wellcode=?", para);
		System.out.println(list.get(0).toJson());
		renderJson(JSON.toJSONString(list.get(0).toJson()));

	}

	public void treeTable() {
		String para = null;
		// para = URLDecoder.decode(getPara("wellcode1"), "utf-8");
		String tableName = getPara("table");
		//System.out.println(tableName);
		int page = Integer.parseInt(getPara("page"));
		Page<Record> list = null;
		para = getPara("wellname1");
		System.out.println(para);
		list = service.ColumnService(tableName, page, para);
		renderJson(list);
	}
	public void adQuery() {
		String para = getPara("tiaojian2");
		String table = getPara("table");
		List<Record> list = service.BQueryService(para,table);
			renderJson(list);
		}


	public void advance1() {
	      String para=getPara("para");
	      List<Record> list = service.AdvanceQueryService1(para);
	     renderJson(list);
		}
	public void advance() {
      String para=getPara("para");
      int pageno=Integer.parseInt(getPara("page"));
      Page<Record> page = service.AdvanceQueryService(para, pageno);
      renderJson(page);
	}
	
	public void  tongji(){
		String tablename=getPara("tablename");
		System.out.println(tablename);
		String sql="select * from"+" "+tablename;
		List<Record> list = Db.find(sql);
		renderJson(list);
	}
	public void  biti(){
		String tablename=getPara("tablename");
		String sql="select * from data_dictionary where tablename=?";
		List<Record> list = Db.find(sql,tablename);
		renderJson(list);
	}
}
